package com.yonyou.brigade.order_center.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.brigade.basic_init.enums.ChannelEnum;
import com.yonyou.brigade.order_center.model.*;
import com.yonyou.brigade.util.operateutil.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrderCenterConvert {
    private static Logger logger = LoggerFactory.getLogger(OrderCenterConvert.class);

    public static BriHotelOrder getHotelOrderByJSON(JSONObject orderJson)  {
        BriHotelOrder hotelOrder = new BriHotelOrder();
        hotelOrder.setOrderNo(orderJson.getString("orderNo"));
        hotelOrder.setTravelType(orderJson.getString("travelType"));
        hotelOrder.setPublicOrPrivate(orderJson.getString("publicOrPrivate"));
        hotelOrder.setIsDomc(orderJson.getString("isDomc"));
        hotelOrder.setPreiumReason(orderJson.getString("preiumReason"));
        hotelOrder.setPreiumDetail(orderJson.getString("preiumDetail"));
        hotelOrder.setStatusType(orderJson.getString("statusType"));
        hotelOrder.setYxDate(orderJson.getDate("yxDate"));
        hotelOrder.setSavePrice(orderJson.getBigDecimal("savePrice"));
        hotelOrder.setCreateBy(orderJson.getString("createBy"));
        hotelOrder.setOrderTime(orderJson.getDate("orderTime"));
        hotelOrder.setOrderStatus(orderJson.getString("orderStatus"));
        hotelOrder.setOrderStatusName(orderJson.getString("OrderStatusName"));
        hotelOrder.setPaymentPrice(orderJson.getBigDecimal("paymentPrice"));
        hotelOrder.setHotelName(orderJson.getString("hotelName"));
        hotelOrder.setHotelId(orderJson.getString("hotelId"));
        hotelOrder.setRoomName(orderJson.getString("roomName"));
        hotelOrder.setRoomTypeId(orderJson.getString("roomTypeId"));
        hotelOrder.setRatePlanName(orderJson.getString("ratePlanName"));
        hotelOrder.setRatePlanId(orderJson.getString("ratePlanId"));
        hotelOrder.setRoomNums(orderJson.getInteger("roomNums"));
        hotelOrder.setArrivalDate(orderJson.getDate("arrivalDate"));
        if (ChannelEnum.CLYH.getName().equals(orderJson.getString("fromType"))){
            hotelOrder.setInDays(getIntegerFromString(orderJson.getString("inDays")));
            hotelOrder.setInNights(getIntegerFromString(orderJson.getString("inNights")));
            //差旅壹号没有离店时间，但生成记事时要求必须有离店时间，所以需要通过计算得到
            if (hotelOrder.getArrivalDate() != null){
                Date arrivalDate = hotelOrder.getArrivalDate();
                String departureDateStr = DateUtils.plusDay(hotelOrder.getInDays(), arrivalDate);
                if (departureDateStr != null){
                    hotelOrder.setDepartureDate(DateUtils.strToDate(departureDateStr));
                }
            }
        } else {
            hotelOrder.setDepartureDate(orderJson.getDate("departureDate"));
            hotelOrder.setInDays(orderJson.getInteger("inDays"));
            hotelOrder.setInNights(orderJson.getInteger("inNights"));
        }
        hotelOrder.setLatestArrivalTime(orderJson.getDate("latestArrivalTime"));
        hotelOrder.setHotelAddress(orderJson.getString("hotelAddress"));
        hotelOrder.setHotelTel(orderJson.getString("hotelTel"));
        hotelOrder.setCreateTime(orderJson.getDate("createTime"));
        hotelOrder.setPayType(orderJson.getString("payType"));
        hotelOrder.setIsGuarantee(orderJson.getInteger("isGuarantee"));
        hotelOrder.setGuaranteeInfo(orderJson.getString("guaranteeInfo"));
        hotelOrder.setContactName(orderJson.getString("contactName"));
        hotelOrder.setContactMobile(orderJson.getString("contactMobile"));
        hotelOrder.setIsAllowCancel(orderJson.getInteger("isAllowCancel"));
        hotelOrder.setCancelTime(orderJson.getDate("cancelTime"));
        hotelOrder.setGcjLat(orderJson.getBigDecimal("gcjLat"));
        hotelOrder.setGcjLon(orderJson.getBigDecimal("gcjLon"));
        hotelOrder.setBaiduLat(orderJson.getBigDecimal("baiduLat"));
        hotelOrder.setBaiduLon(orderJson.getBigDecimal("baiduLon"));
        hotelOrder.setVerifyType(orderJson.getString("verifyType"));
        hotelOrder.setHeadUserName(orderJson.getString("headUserName"));
        hotelOrder.setFlowBaseId(orderJson.getString("flowBaseId"));
        hotelOrder.setIsAllowCheck(orderJson.getString("isAllowCheck"));
        hotelOrder.setPenaltyToCustomer(orderJson.getString("penaltyToCustomer"));
        hotelOrder.setPenaltyCurrencyCode(orderJson.getString("penaltyCurrencyCode"));
        hotelOrder.setPrepayRuleInfo(orderJson.getString("prepayRuleInfo"));
        hotelOrder.setPassengerType(orderJson.getString("passengerType"));
        hotelOrder.setPaymentType(orderJson.getString("paymentType"));
        hotelOrder.setInvoiceMode(orderJson.getString("invoiceMode"));
        hotelOrder.setSumServiceCharge(orderJson.getBigDecimal("sumServiceCharge"));
        hotelOrder.setApplyOrderNo(orderJson.getString("applyOrderNo"));
        hotelOrder.setFromType(orderJson.getString("fromType"));
        hotelOrder.setCardHolderName(orderJson.getString("cardHolderName"));
        hotelOrder.setHotelType(orderJson.getString("hotelType"));
        hotelOrder.setHotelCity(orderJson.getString("hotelCity"));
        hotelOrder.setHotelStars(orderJson.getString("hotelStars"));
        hotelOrder.setCurrency(orderJson.getString("currency"));
        hotelOrder.setPreOrderMode(orderJson.getString("preOrderMode"));
        hotelOrder.setRelateYBZID(orderJson.getString("relateYBZID"));
        hotelOrder.setRelateOtherID(orderJson.getString("relateOtherID"));
        hotelOrder.setBxStatus(StringUtils.isEmpty(orderJson.getString("bxStatus")) ? 101 : Integer.valueOf(orderJson.getString("bxStatus")));
        hotelOrder.setAuthStatus(orderJson.getString("authStatus"));
        hotelOrder.setNote(orderJson.getString("note"));
        hotelOrder.setHotelLevel(orderJson.getString("hotelLevel"));
        hotelOrder.setChannelId(orderJson.getString("channelId"));
        hotelOrder.setCategory(orderJson.getString("category"));
        hotelOrder.setParentOrderNo(orderJson.getString("parentOrderNo"));
        return hotelOrder;
    }

    private static Integer getIntegerFromString(String inDays) {
        int result = 0;
        if (inDays != null){
            double aDouble = Double.valueOf(inDays);
            int i = (int) aDouble;
            result = Integer.valueOf(i);
        }
        return result;
    }

    public static List<BriHotelRoom> getBriHotelRoomByJSON(JSONObject orderJson, String orderNO) {
        List<BriHotelRoom> hotelRooms = new ArrayList<>();
        JSONArray roomInfos = orderJson.getJSONArray("roomInfo");
        if (roomInfos != null && roomInfos.size() > 0){
            for (int i = 0; i < roomInfos.size(); i++) {
                BriHotelRoom briHotelRoom = new BriHotelRoom();
                JSONObject jsonObject = roomInfos.getJSONObject(i);
                briHotelRoom.setOrderNO(orderNO);
                briHotelRoom.setPsgName(jsonObject.getString("psgName"));
                briHotelRoom.setOAUserCode(jsonObject.getString("OAUserCode"));
                briHotelRoom.setPersonMobile(jsonObject.getString("personMobile"));
                briHotelRoom.setCostCenter(jsonObject.getString("costCenter"));
                briHotelRoom.setIsRuleOver(jsonObject.getString("isRuleOver"));
                briHotelRoom.setNoTravelRules(jsonObject.getString("noTravelRules"));
                briHotelRoom.setServiceFee(jsonObject.getBigDecimal("serviceFee"));
                briHotelRoom.setPsgNums(getIntegerVal(jsonObject, "psgNums"));
                briHotelRoom.setCostCenter2(jsonObject.getString("costCenter2"));
                briHotelRoom.setCostCenter3(jsonObject.getString("costCenter3"));
                briHotelRoom.setRoomName(jsonObject.getString("roomName"));
                briHotelRoom.setRoomTypeId(jsonObject.getString("roomTypeId"));
                hotelRooms.add(briHotelRoom);
            }
        }
        return hotelRooms;
    }

    public static BriFlightOrder getFlightOrderByJSON(JSONObject orderJson) {
        BriFlightOrder flightOrder = new BriFlightOrder();
        flightOrder.setFromType(orderJson.getString("fromType"));
        flightOrder.setChannelId(orderJson.getString("channelId"));
        flightOrder.setOrderNo(orderJson.getString("orderNo"));
        flightOrder.setOrderType(orderJson.getString("orderType"));
        flightOrder.setPublicOrPrivate(orderJson.getString("publicOrPrivate"));
        flightOrder.setIsDomc(orderJson.getString("isDomc"));
        flightOrder.setPreiumReason(orderJson.getString("preiumReason"));
        flightOrder.setPreiumDetail(orderJson.getString("preiumDetail"));
        flightOrder.setOrderStatus(orderJson.getString("orderStatus"));
        flightOrder.setPaymentPrice(orderJson.getBigDecimal("paymentPrice"));
        flightOrder.setApplyOrderNo(orderJson.getString("applyOrderNo"));
        flightOrder.setFarePrice(orderJson.getBigDecimal("farePrice"));
        flightOrder.setJsonOtherInfo(orderJson.getString("jsonOtherInfo"));
        flightOrder.setTaxPrice(orderJson.getBigDecimal("taxPrice"));
        flightOrder.setPsgCount(getIntegerVal(orderJson, "psgCount"));
        flightOrder.setStatusType(orderJson.getString("statusType"));
        flightOrder.setRefundPrice(orderJson.getBigDecimal("refundPrice"));
        flightOrder.setTpPrice(orderJson.getBigDecimal("tpPrice"));
        flightOrder.setTripType(orderJson.getString("tripType"));
        flightOrder.setIsCheck(orderJson.getString("isCheck"));
        flightOrder.setIsAllowRefund(orderJson.getString("isAllowRefund"));
        flightOrder.setAllowPayDate(orderJson.getDate("allowPayDate"));
        flightOrder.setRefundMark(orderJson.getString("refundMark"));
        flightOrder.setVerifyType(orderJson.getString("verifyType"));
        flightOrder.setHeadUserName(orderJson.getString("headUserName"));
        flightOrder.setFlowBaseId(orderJson.getString("flowBaseId"));
        flightOrder.setIsAllowCheck(orderJson.getString("isAllowCheck"));
        flightOrder.setSumCounterFee(orderJson.getBigDecimal("sumCounterFee"));
        flightOrder.setSumInsMoney(orderJson.getBigDecimal("sumInsMoney"));
        flightOrder.setRelatedNo(orderJson.getString("relatedNo"));
        flightOrder.setRelatedType(orderJson.getString("relatedType"));
        flightOrder.setIsPurchase(orderJson.getString("isPurchase"));
        flightOrder.setPurchaseCustName(orderJson.getString("purchaseCustName"));
        flightOrder.setPurchaseVerify(orderJson.getString("purchaseVerify"));
        flightOrder.setIsHs(orderJson.getString("isHs"));
        flightOrder.setHsTitle(orderJson.getString("hsTitle"));
        flightOrder.setSavePrice(orderJson.getBigDecimal("savePrice"));
        flightOrder.setCreateBy(orderJson.getString("createBy"));
        flightOrder.setOrderTime(orderJson.getDate("orderTime"));
        flightOrder.setYxDate(orderJson.getDate("yxDate"));
        flightOrder.setFromType(orderJson.getString("fromType"));
        flightOrder.setPayType(orderJson.getString("payType"));
        flightOrder.setSendTicketFee(orderJson.getBigDecimal("sendTicketFee"));
        flightOrder.setChangeFee(orderJson.getBigDecimal("changeFee"));
        flightOrder.setCurrency(orderJson.getString("currency"));
        flightOrder.setNoPreOrderReason(orderJson.getString("noPreOrderReason"));
        flightOrder.setNoOrderLowstReason(orderJson.getString("noOrderLowstReason"));
        flightOrder.setRelateYbzId(orderJson.getString("relateYbzId"));
        flightOrder.setRelateOtherId(orderJson.getString("relateOtherId"));
        flightOrder.setPreOrderMode(orderJson.getString("preOrderMode"));
        flightOrder.setAuthStatus(orderJson.getString("authStatus"));
        flightOrder.setNote(orderJson.getString("note"));
        flightOrder.setPaymentType(orderJson.getString("paymentType"));
        flightOrder.setCategory(orderJson.getString("category"));
        flightOrder.setParentOrderNo(orderJson.getString("parentOrderNo"));
        return flightOrder;
    }

    public static Integer getIntegerVal(JSONObject orderJson, String key){
        Integer result = null;
        try {
            result = orderJson.getInteger(key);
        }catch (Exception e){

        }
        if (result == null){
            try {
                result = orderJson.getBigDecimal(key).toBigInteger().intValue();
            }catch (Exception e){

            }
        }
        return result;
    }


    public static BriTrainOrder getTrainOrderByJSON(JSONObject orderJson) {
        BriTrainOrder trainOrder = new BriTrainOrder();
        trainOrder.setOrderNo(orderJson.getString("orderNo"));
        trainOrder.setPublicOrPriveate(orderJson.getString("publicOrPriveate"));
        trainOrder.setIsDomc(orderJson.getString("isDomc"));
        trainOrder.setPreiumReason(orderJson.getString("preiumReason"));
        trainOrder.setPreiumDetail(orderJson.getString("preiumDetail"));
        trainOrder.setStatusType(orderJson.getString("statusType"));
        trainOrder.setYxDate(orderJson.getDate("yxDate"));
        trainOrder.setSavePrice(orderJson.getBigDecimal("savePrice"));
        trainOrder.setCreateBy(orderJson.getString("createBy"));
        trainOrder.setUserId(orderJson.getString("userId"));
        trainOrder.setOrderTime(orderJson.getDate("orderTime"));
        trainOrder.setOrderStatus(orderJson.getString("orderStatus"));
        trainOrder.setPaymentPrice(orderJson.getBigDecimal("paymentPrice"));
        trainOrder.setLinkName(orderJson.getString("linkName"));
        trainOrder.setLinkPhone(orderJson.getString("linkPhone"));
        trainOrder.setCancelReason(orderJson.getString("cancelReason"));
        trainOrder.setPayLimitTime(orderJson.getDate("payLimitTime"));
        trainOrder.setPayLimitStatus(orderJson.getString("payLimitStatus"));
        trainOrder.setRefundSwiftNo(orderJson.getString("isAllowRefund"));
        trainOrder.setJsonOtherInfo(orderJson.getString("jsonOtherInfo"));
        trainOrder.setAllowPayDate(orderJson.getDate("allowPayDate"));
        trainOrder.setIsCheck(orderJson.getString("isCheck"));
        trainOrder.setCheckStatus(orderJson.getString("checkStatus"));
        trainOrder.setOrderType(orderJson.getString("orderType"));
        trainOrder.setRefundPrice(orderJson.getBigDecimal("refundPrice"));
        trainOrder.setTpPrice(orderJson.getBigDecimal("tpPrice"));
        trainOrder.setSumCounterFee(orderJson.getBigDecimal("sumCounterFee"));
        trainOrder.setCarrierSumFee(orderJson.getBigDecimal("carrierSumFee"));
        trainOrder.setIsSupplement(getIntegerVal(orderJson, "isSupplement"));
        trainOrder.setVerifyType(orderJson.getString("verifyType"));
        trainOrder.setHeadUserName(orderJson.getString("headUserName"));
        trainOrder.setFlowBaseId(orderJson.getString("flowBaseId"));
        trainOrder.setIsAllowCheck(orderJson.getString("isAllowCheck"));
        trainOrder.setOrderStatusName(orderJson.getString("orderStatusName"));
        trainOrder.setTrainOrderNo(orderJson.getString("trainOrderNo"));
        trainOrder.setParentOrderNo(orderJson.getString("parentOrderNo"));
        trainOrder.setApplyOrderNo(orderJson.getString("applyOrderNo"));
        trainOrder.setCurrency(orderJson.getString("currency"));
        trainOrder.setPaymentType(orderJson.getString("paymentType"));
        trainOrder.setPreOrderMode(orderJson.getString("preOrderMode"));
        trainOrder.setRelateYBZID(orderJson.getString("relateYBZID"));
        trainOrder.setRelateOtherID(orderJson.getString("relateOtherID"));
        trainOrder.setAuthStatus(orderJson.getString("authStatus"));
        trainOrder.setBxStatus(getIntegerVal(orderJson, "bxStatus"));
        trainOrder.setNote(orderJson.getString("note"));
        trainOrder.setCardHolderName(orderJson.getString("cardHolderName"));
        trainOrder.setTicketsNum(getIntegerVal(orderJson, "ticketsNum"));
        trainOrder.setPayType(orderJson.getString("payType"));
        trainOrder.setFromType(orderJson.getString("fromType"));
        trainOrder.setPaperTicketFee(orderJson.getBigDecimal("paperTicketFee"));
        trainOrder.setChannelId(orderJson.getString("channelId"));
        trainOrder.setCategory(orderJson.getString("category"));
        return trainOrder;
    }

    public static List<BriFlightInfo> getBriFlightInfoByJSON(JSONObject orderJson, String orderNo) throws ParseException {
        List<BriFlightInfo> flightInfos = new ArrayList<>();
        JSONArray flightInfo = orderJson.getJSONArray("flightInfo");
        if (flightInfo != null && flightInfo.size() > 0){
            for (int i = 0; i < flightInfo.size(); i++) {
                JSONObject flightInfoJSONObject = flightInfo.getJSONObject(i);
                BriFlightInfo briFlightInfo = new BriFlightInfo();
                briFlightInfo.setOrderNo(orderNo);
                briFlightInfo.setCarrierName(flightInfoJSONObject.getString("carrierName"));
                briFlightInfo.setBoardPoint(flightInfoJSONObject.getString("boardPoint"));
                briFlightInfo.setBoardPointName(flightInfoJSONObject.getString("boardPointName"));
                briFlightInfo.setBoardPointAirPortName(flightInfoJSONObject.getString("boardPointAirPortName"));
                briFlightInfo.setBoardPointAt(flightInfoJSONObject.getString("boardPointAt"));
                briFlightInfo.setDepartureDate(flightInfoJSONObject.getDate("departureDate"));
                if (ChannelEnum.TUNIU.getName().equals(orderJson.getString("fromType"))) {
                    briFlightInfo.setDepartureTime(DateUtils.strToTime(flightInfoJSONObject.getString("departureTime")));
                    briFlightInfo.setArriveTime(DateUtils.strToTime(flightInfoJSONObject.getString("arriveTime")));
                }else {
                    if (isValidLong(flightInfoJSONObject.getString("departureDate"))) {
                        briFlightInfo.setDepartureTime(DateUtils.dateToTime(new Date(Long.valueOf(flightInfoJSONObject.getString("departureDate")))));
                        briFlightInfo.setArriveTime(DateUtils.dateToTime(new Date(Long.valueOf(flightInfoJSONObject.getString("arriveDate")))));
                    } else {
                        briFlightInfo.setDepartureTime(getTimeFromDateTime(flightInfoJSONObject.getString("departureDate")));
                        briFlightInfo.setArriveTime(getTimeFromDateTime(flightInfoJSONObject.getString("arriveDate")));
                        if (briFlightInfo.getDepartureTime() == null) {
                            briFlightInfo.setDepartureTime(DateUtils.strToTime(flightInfoJSONObject.getString("departureTime")));
                        }
                        if (briFlightInfo.getArriveTime() == null) {
                            briFlightInfo.setArriveTime(DateUtils.strToTime(flightInfoJSONObject.getString("arriveTime")));
                        }
                    }
                }
                briFlightInfo.setOffPoint(flightInfoJSONObject.getString("offPoint"));
                briFlightInfo.setOffPointName(flightInfoJSONObject.getString("offPointName"));
                briFlightInfo.setOffPointAirPortName(flightInfoJSONObject.getString("offPointAirPortName"));
                briFlightInfo.setOffPointAt(flightInfoJSONObject.getString("offPointAt"));
                briFlightInfo.setArriveDate(flightInfoJSONObject.getDate("arriveDate"));
                briFlightInfo.setFlightNo(flightInfoJSONObject.getString("flightNo"));
                briFlightInfo.setAircraft(flightInfoJSONObject.getString("aircraft"));
                briFlightInfo.setTimeSpan(flightInfoJSONObject.getString("timeSpan"));
                briFlightInfo.setTgq(flightInfoJSONObject.getString("tgq"));
                briFlightInfo.setYprice(flightInfoJSONObject.getString("yprice"));
                briFlightInfo.setClassRebate(flightInfoJSONObject.getString("classRebate"));
                briFlightInfo.setClassCode(flightInfoJSONObject.getString("classCode"));
                briFlightInfo.setClassLevelCode(flightInfoJSONObject.getString("classLevelCode"));
                briFlightInfo.setClassLevelName(flightInfoJSONObject.getString("classLevelName"));
                briFlightInfo.setFromType(flightInfoJSONObject.getString("fromType"));
                briFlightInfo.setLowestPrice(flightInfoJSONObject.getBigDecimal("lowestPrice"));
                briFlightInfo.setOilfee(flightInfoJSONObject.getBigDecimal("oilfee"));
                briFlightInfo.setConstructionPrice(flightInfoJSONObject.getBigDecimal("constructionPrice"));
                flightInfos.add(briFlightInfo);
            }
        }
        return flightInfos;
    }

    public static Time getTimeFromDateTime(String dateTime)  {
        Time time = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if (dateTime != null) {
                time = new Time(format.parse(dateTime).getTime());
            }
        } catch (ParseException e) {
            logger.error("OrderConvert中时间拆分获取时间出现错误！",e);
            //throw e;
        }
        return time;
    }

    public static List<BriFlightPassenger> getBriFlightPassengerByJSON(JSONObject orderJson, String orderNo) {
        List<BriFlightPassenger> flightPassengers = new ArrayList<>();
        JSONArray flightPassenger = orderJson.getJSONArray("flightPassenger");
        if (flightPassenger != null && flightPassenger.size() > 0){
            for (int i = 0; i < flightPassenger.size(); i++) {
                JSONObject passengerJSONObject = flightPassenger.getJSONObject(i);
                BriFlightPassenger briFlightPassenger = new BriFlightPassenger();
                briFlightPassenger.setOrderNo(orderNo);
                briFlightPassenger.setPsgName(passengerJSONObject.getString("psgName"));
                briFlightPassenger.setOAUserCode(passengerJSONObject.getString("OAUserCode"));
                briFlightPassenger.setCardType(passengerJSONObject.getString("cardType"));
                briFlightPassenger.setCardNo(passengerJSONObject.getString("cardNo"));
                briFlightPassenger.setCostCenter(passengerJSONObject.getString("costCenter"));
                briFlightPassenger.setTicketNo(passengerJSONObject.getString("ticketNo"));
                briFlightPassenger.setTicketStatus(passengerJSONObject.getString("ticketStatus"));
                briFlightPassenger.setDepartmentName(passengerJSONObject.getString("departmentName"));
                briFlightPassenger.setIsAllowRefund(passengerJSONObject.getString("isAllowRefund"));
                briFlightPassenger.setPsgRefundPrice(passengerJSONObject.getBigDecimal("psgRefundPrice"));
                briFlightPassenger.setPsgTpPrice(passengerJSONObject.getBigDecimal("psgTpPrice"));
                briFlightPassenger.setCounterFee(passengerJSONObject.getBigDecimal("counterFee"));
                briFlightPassenger.setIsRuleOver(passengerJSONObject.getString("isRuleOver"));
                briFlightPassenger.setNoTravelRules(passengerJSONObject.getString("noTravelRules"));
                briFlightPassenger.setTpCounterFee(passengerJSONObject.getBigDecimal("tpCounterFee"));
                briFlightPassenger.setInsId(passengerJSONObject.getString("insId"));
                briFlightPassenger.setInsName(passengerJSONObject.getString("insName"));
                briFlightPassenger.setInsCount(getIntegerVal(passengerJSONObject, "insCount"));
                briFlightPassenger.setInsMoney(passengerJSONObject.getBigDecimal("insMoney"));
                briFlightPassenger.setPurchaseBank(passengerJSONObject.getString("purchaseBank"));
                briFlightPassenger.setIsCusPsg(passengerJSONObject.getString("isCusPsg"));
                briFlightPassenger.setPsgLevel(passengerJSONObject.getString("psgLevel"));
                briFlightPassenger.setBindCounterFee(passengerJSONObject.getBigDecimal("bindCounterFee"));
                briFlightPassenger.setConsturctAndFuel(passengerJSONObject.getBigDecimal("consturctAndFuel"));
                briFlightPassenger.setDepartment1(passengerJSONObject.getString("department1"));
                briFlightPassenger.setDepartment2(passengerJSONObject.getString("department2"));
                briFlightPassenger.setDepartment3(passengerJSONObject.getString("department3"));
                briFlightPassenger.setCostCenter2(passengerJSONObject.getString("costCenter2"));
                briFlightPassenger.setCostCenter3(passengerJSONObject.getString("costCenter3"));
                briFlightPassenger.setTicketNoT(passengerJSONObject.getString("ticketNoT"));
                flightPassengers.add(briFlightPassenger);
            }
        }
        return flightPassengers;
    }

    public static List<BriFlightOrderContact> getBriFlightOrderContactByJSON(JSONObject orderJson, String orderNo) {
        List<BriFlightOrderContact> flightOrderContacts = new ArrayList<>();
        JSONArray flightContact = orderJson.getJSONArray("flightContact");
        if (flightContact != null && flightContact.size() > 0){
            for (int i = 0; i < flightContact.size(); i++) {
                JSONObject contactJSONObject = flightContact.getJSONObject(i);
                BriFlightOrderContact briFlightOrderContact = new BriFlightOrderContact();
                briFlightOrderContact.setOrderNo(orderNo);
                briFlightOrderContact.setContactName(contactJSONObject.getString("contactName"));
                briFlightOrderContact.setContactMobile(contactJSONObject.getString("contactMobile"));
                flightOrderContacts.add(briFlightOrderContact);
            }
        }
        return flightOrderContacts;
    }

    public static BriTrainPassenger getBriTrainPassengerByJSON(JSONObject passsenger, String orderNo) {
        BriTrainPassenger briTrainPassenger = new BriTrainPassenger();
        briTrainPassenger.setOrderNo(orderNo);
        briTrainPassenger.setPsgName(passsenger.getString("psgName"));
        briTrainPassenger.setOAUserCode(passsenger.getString("OAUserCode"));
        briTrainPassenger.setTicketType(getIntegerVal(passsenger, "ticketType"));
        briTrainPassenger.setCardType(passsenger.getString("cardType"));
        briTrainPassenger.setCardNo(passsenger.getString("cardNo"));
        briTrainPassenger.setCostCenter(passsenger.getString("costCenter"));
        briTrainPassenger.setCostCenter2(passsenger.getString("costCenter2"));
        briTrainPassenger.setCostCenter3(passsenger.getString("costCenter3"));
        briTrainPassenger.setSeatType(passsenger.getString("seatType"));
        briTrainPassenger.setTrainBox(passsenger.getString("trainBox"));
        briTrainPassenger.setSeatNo(passsenger.getString("seatNo"));
        briTrainPassenger.setTktPrice(passsenger.getBigDecimal("tktPrice"));
        briTrainPassenger.setPsgStatus(passsenger.getString("psgStatus"));
        briTrainPassenger.setRefundAmount(passsenger.getBigDecimal("refundAmount"));
        briTrainPassenger.setTpPrice(passsenger.getBigDecimal("tpPrice"));
        briTrainPassenger.setCounterFee(passsenger.getBigDecimal("counterFee"));
        briTrainPassenger.setCarrierChangeFee(passsenger.getBigDecimal("carrierChangeFee"));
        briTrainPassenger.setIsRuleOver(passsenger.getString("isRuleOver"));
        briTrainPassenger.setNoTravelRules(passsenger.getString("noTravelRules"));
        briTrainPassenger.setIsAllowRefund(passsenger.getString("isAllowRefund"));
        briTrainPassenger.setPsgNo(passsenger.getString("psgNo"));
        briTrainPassenger.setIsCusPsg(passsenger.getString("isCusPsg"));
        briTrainPassenger.setPersonMobile(passsenger.getString("personMobile"));
        briTrainPassenger.setUserId(passsenger.getString("userId"));
        briTrainPassenger.setPsgPosition(passsenger.getString("psgPosition"));
        briTrainPassenger.setSumInsMoney(passsenger.getBigDecimal("sumInsMoney"));
        briTrainPassenger.setSendTicketFee(passsenger.getBigDecimal("sendTicketFee"));
        briTrainPassenger.setDepartment1(passsenger.getString("department1"));
        briTrainPassenger.setDepartment2(passsenger.getString("department2"));
        briTrainPassenger.setDepartment3(passsenger.getString("department3"));
        briTrainPassenger.setTicketsType(passsenger.getString("ticketsType"));
        return briTrainPassenger;
    }

    public static List<BriTrainPayment> getBriTrainPaymentByJSON(JSONObject orderJson, String orderNo) {
        ArrayList<BriTrainPayment> briTrainPayments = new ArrayList<>();
        JSONArray payments = orderJson.getJSONArray("payments");
        if (payments != null && payments.size() > 0) {
            for (int i = 0; i < payments.size(); i++) {
                JSONObject paymentJSONObject = payments.getJSONObject(i);
                BriTrainPayment briTrainPayment = new BriTrainPayment();
                briTrainPayment.setOrderNo(orderNo);
                briTrainPayment.setCurrency(paymentJSONObject.getString("currency"));
                briTrainPayment.setAmount(paymentJSONObject.getBigDecimal("amount"));
                briTrainPayment.setExchangeRate(paymentJSONObject.getBigDecimal("exchangeRate"));
                briTrainPayment.setDealType(paymentJSONObject.getString("dealType"));
                briTrainPayment.setPayType(paymentJSONObject.getString("payType"));
                briTrainPayment.setPayRemark(paymentJSONObject.getString("payRemark"));
                briTrainPayment.setVoucherPayAmount(paymentJSONObject.getBigDecimal("voucherPayAmount"));
                briTrainPayment.setPayCategory(paymentJSONObject.getString("payCategory"));
                briTrainPayments.add(briTrainPayment);
            }
        }
        return briTrainPayments;
    }

    public static List<BriTrainTicket> getBriTrainTicketByJSON(JSONArray tickets, String orderNo, String cardNo) {
        ArrayList<BriTrainTicket> briTrainTickets = new ArrayList<>();
//        JSONArray tickets = orderJson.getJSONArray("tickets");
        if (tickets != null && tickets.size() > 0) {
            for (int i = 0; i < tickets.size(); i++) {
                JSONObject ticketJSONObject = tickets.getJSONObject(i);
                BriTrainTicket briTrainTicket = new BriTrainTicket();
                briTrainTicket.setOrderNo(orderNo);
                briTrainTicket.setCardNo(cardNo);
                briTrainTicket.setTicketInfoId(ticketJSONObject.getString("ticketInfoId"));
                briTrainTicket.setTrainCode(ticketJSONObject.getString("trainCode"));
                briTrainTicket.setTrainCodeName(ticketJSONObject.getString("trainCodeName"));
                briTrainTicket.setTicketType(ticketJSONObject.getString("ticketType"));
                briTrainTicket.setFromStation(ticketJSONObject.getString("fromStation"));
                briTrainTicket.setToStation(ticketJSONObject.getString("toStation"));
                briTrainTicket.setTrainDate(ticketJSONObject.getDate("trainDate"));
                if (isValidLong(ticketJSONObject.getString("fromTime"))) {
                    briTrainTicket.setFromTime(DateUtils.dateToTime(new Date(Long.valueOf(ticketJSONObject.getString("fromTime")))));
                    briTrainTicket.setToTime(DateUtils.dateToTime(new Date(Long.valueOf(ticketJSONObject.getString("toTime")))));
                } else {
                    briTrainTicket.setFromTime(DateUtils.strToTime(ticketJSONObject.getString("fromTime")));
                    briTrainTicket.setToTime(DateUtils.strToTime(ticketJSONObject.getString("toTime")));
                }
                briTrainTicket.setToDate(ticketJSONObject.getDate("toDate"));
                briTrainTicket.setSeatType(ticketJSONObject.getString("seatType"));
                briTrainTicket.setTunTime(ticketJSONObject.getString("tunTime"));
                briTrainTicket.setIsAllowRefund(ticketJSONObject.getString("isAllowRefund"));
                briTrainTicket.setRefundAmount(ticketJSONObject.getBigDecimal("refundAmount"));
                briTrainTicket.setTpPrice(ticketJSONObject.getBigDecimal("tpPrice"));
                briTrainTicket.setTrainOrderNo(ticketJSONObject.getString("trainOrderNo"));
                briTrainTicket.setTrainBox(ticketJSONObject.getString("trainBox"));
                briTrainTicket.setSeatNo(ticketJSONObject.getString("seatNo"));
                briTrainTicket.setTktPrice(ticketJSONObject.getBigDecimal("tktPrice"));
                briTrainTicket.setRefundTicketStatus(ticketJSONObject.getString("refundTicketStatus"));
                briTrainTicket.setRefundMark(ticketJSONObject.getString("refundMark"));
                briTrainTicket.setChangeStatus(ticketJSONObject.getString("changeStatus"));
                briTrainTicket.setCounterFee(ticketJSONObject.getBigDecimal("counterFee"));
                briTrainTicket.setCarrierChangeFee(ticketJSONObject.getBigDecimal("carrierChangeFee"));
                briTrainTicket.setSendTicketFee(ticketJSONObject.getBigDecimal("sendTicketFee"));
                briTrainTickets.add(briTrainTicket);
            }
        }
        return briTrainTickets;
    }

    public static BriCarOrder getBriCarOrderFromJSON(JSONObject orderJson) {
        BriCarOrder briCar = new BriCarOrder();
        briCar.setOrderNo(orderJson.getString("orderNo"));
        briCar.setType(orderJson.getString("type"));
        briCar.setPublicOrPrivate(orderJson.getString("publicOrPrivate"));
        briCar.setIsDomc(orderJson.getString("isDomc"));
        briCar.setOrderStatus(orderJson.getString("orderStatus"));
        briCar.setVehicleModel(orderJson.getString("vehicleModel"));
        briCar.setStartName(orderJson.getString("startName"));
        briCar.setEndName(orderJson.getString("endName"));
        briCar.setDepartureTime(orderJson.getDate("departureTime"));
        briCar.setArriveTime(orderJson.getDate("arriveTime"));
        briCar.setCallPhone(orderJson.getString("callPhone"));
        briCar.setPsgName(orderJson.getString("psgName"));
        briCar.setPsgPhone(orderJson.getString("psgPhone"));
        briCar.setLinkerName(orderJson.getString("linkerName"));
        briCar.setLinkerPhone(orderJson.getString("linkerPhone"));
        briCar.setDriverPhone(orderJson.getString("driverPhone"));
        briCar.setVehicleCard(orderJson.getString("vehicleCard"));
        briCar.setCreateBy(orderJson.getString("createBy"));
        briCar.setPayType(orderJson.getString("payType"));
        briCar.setPaymentType(orderJson.getString("paymentType"));
        briCar.setCityName(orderJson.getString("cityName"));
        briCar.setSourceType(orderJson.getString("sourceType"));
        briCar.setPlatCode(orderJson.getString("platCode"));
        briCar.setOutUserCode(orderJson.getString("outUserCode"));
        briCar.setApproveStatus(orderJson.getString("approveStatus"));
        briCar.setDistance(orderJson.getString("distance"));
        briCar.setDuration(orderJson.getString("duration"));
        briCar.setOtherInfo(orderJson.getString("otherInfo"));
        briCar.setOutCompanyId(orderJson.getString("outCompanyId"));
        briCar.setOutCreateTime(orderJson.getDate("outCreateTime"));
        briCar.setTotalPrice(orderJson.getBigDecimal("totalPrice"));
        briCar.setActualPrice(orderJson.getBigDecimal("actualPrice"));
        briCar.setCouponPay(orderJson.getBigDecimal("couponPay"));
        briCar.setCompanyRealPay(orderJson.getBigDecimal("companyRealPay"));
        briCar.setPersonRealPay(orderJson.getBigDecimal("personRealPay"));
        briCar.setArrearsPay(orderJson.getBigDecimal("arrearsPay"));
        briCar.setCompanyRealRefund(orderJson.getBigDecimal("companyRealRefund"));
        briCar.setPersonRealRefund(orderJson.getBigDecimal("personRealRefund"));
        briCar.setPayTime(orderJson.getDate("payTime"));
        briCar.setInApproveId(orderJson.getString("inApproveId"));
        briCar.setEmployeeNumber(orderJson.getString("employeeNumber"));
        briCar.setUseCarConfigId(orderJson.getString("useCarConfigId"));
        briCar.setIsInvoice(orderJson.getString("isInvoice"));
        briCar.setUseCarSrv(orderJson.getString("useCarSrv"));
        briCar.setInCreateTime(orderJson.getDate("inCreateTime"));
        briCar.setBxState(getIntegerVal(orderJson, "bxState"));
        briCar.setFromType(orderJson.getString("fromType"));
        briCar.setChannelId(getIntegerVal(orderJson, "channelId"));
        briCar.setCategory(orderJson.getString("category"));
        briCar.setArriveCity(orderJson.getString("arriveCity"));
        briCar.setAirPlaneNo(orderJson.getString("airPlaneNo"));
        briCar.setCostCenterName(orderJson.getString("costCenterName"));
        briCar.setRelateOtherId(orderJson.getString("relateOtherId"));
        return briCar;
    }
    public static boolean isValidLong(String str){
        try{
            long _v = Long.parseLong(str);
            return true;
        }catch(NumberFormatException e){
            return false;
        }
    }

}
